import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.activation.MimetypesFileTypeMap;
import javax.naming.NamingException;

import unilearn.domain.*;
import unilearn.facade.*;

/**
 * @author Yin Zhang, 5393558
 * @version 0.1, May 10 2008, initial creation
 */
public class StaffUserStory5
{
	private static PrintWriter fileOutput = null;
	
	private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm:ss");
	private static final String SEPARATELINE = "--------------------------------------------------------";
	private static final String OUTPUTPATH = "staffUserStory5";
	private static final String OUTPUTFILENAME = "staffCreateAssignment.txt";
	private static final long ONEDAY = (long) 1000.0 * 60 * 60 * 24;
	
	public StaffUserStory5() throws FileNotFoundException
	{
		File filePath = new File(OUTPUTPATH);
		
		if(!filePath.exists())
		{
			filePath.mkdir();
		}
		
		fileOutput = new PrintWriter(new FileOutputStream(OUTPUTPATH + File.separator + OUTPUTFILENAME));
	}
	
	public void createNewAssignment(String[] assignmentDetails) throws SQLException, NamingException, IOException
	{
		long assignmentID = -1;
		Assignment assignment = new Assignment();
		assignment.setSubjectID(Long.parseLong(assignmentDetails[0]));
		assignment.setName(assignmentDetails[1]);
		assignment.setDesc(assignmentDetails[2]);
		assignment.setAcceptSubmissions(false);
		assignment.setAcceptMarking(true);
		java.util.Date theDayBeforeYesterday = new java.util.Date(System.currentTimeMillis() - ONEDAY * 2);
		java.util.Date yesterday = new java.util.Date(System.currentTimeMillis() - ONEDAY);
		Timestamp releaseDate = new Timestamp(theDayBeforeYesterday.getTime());
		Timestamp dueDate = new Timestamp(yesterday.getTime());
		assignment.setReleaseDate(releaseDate);
		assignment.setDueDate(dueDate);
		long fileID = submitAssignmentFile(assignmentDetails[3]);
		if(fileID != -1)
		{
			assignment.setFileID(fileID);
			assignmentID = AssignmentFacade.addAssignment(assignment);
			fileOutput.println("Assignment ID " + assignmentID + " added");
			fileOutput.println(SEPARATELINE);
			printAssignment(assignmentID);
		}
		else
		{
			fileOutput.println("assignment File doesn't exist");
		}
	}
	
	private long submitAssignmentFile(String fileName) throws SQLException, NamingException, IOException
	{
		DataFile assignmentFile = new DataFile();
		long fileID = -1;
		assignmentFile.setFileName(fileName);
		File temp = new File(fileName);
		if(temp.exists())
		{
			assignmentFile.setFileSize(temp.length());
			assignmentFile.setFile(temp);
			assignmentFile.setFileType(new MimetypesFileTypeMap().getContentType(temp));
			fileID = FileFacade.insertFile(assignmentFile);
			temp.delete();
		}
		return fileID;
	}
	
	private void printAssignment(long assignmentID) throws SQLException, NamingException, IOException
	{
		Assignment assignment = AssignmentFacade.getAssignmentByID(assignmentID);
		Subject subject = SubjectFacade.getSubjectByID(assignment.getSubjectID());
		String subjectName = subject.getSubjectCode() + " - " + subject.getSubjectName();
		String fileName = "None";
		long fileSize = 0;
		String releaseDate = formatDate(assignment.getReleaseDate());
		String dueDate = formatDate(assignment.getDueDate());
		String accStatus = null;
		
		if(assignment.getFileID() != -1)
		{
			DataFile file = FileFacade.getFileByID(assignment.getFileID());
			fileName = file.getFileName();
			fileSize = file.getFileSize();
		}
		
		if(assignment.getAcceptSubmissions())
		{
			if(assignment.getAcceptMarking())
			{
				accStatus = "open for marking and submission";
			}
			else
			{
				accStatus = "open for submission";
			}
		}
		else
		{
			if(assignment.getAcceptMarking())
			{
				accStatus = "open for marking";
			}
			else
			{
				accStatus = "closed for submission";
			}		
		}
		
		fileOutput.println(subjectName);
		fileOutput.println("Assignment Name: \t\t\t\t\t" + assignment.getName());
		fileOutput.println("Release Date: \t\t\t\t\t\t" + releaseDate);
		fileOutput.println("Due Date: \t\t\t\t\t\t\t\t" + dueDate);
		fileOutput.println("Assignment status: \t\t\t\t" + accStatus);
		fileOutput.println("Assignment Files: \t\t\t\t" + fileName + " - " + fileSize + " bytes");
		fileOutput.println("Assignment Description: \t" + assignment.getDesc());
	}
	
	private static String formatDate(Timestamp timestamp)
	{
        if (timestamp == null) 
        {
            return null;
        }
        else 
        {
            return DATEFORMAT.format((java.util.Date) timestamp);
        }
	}	
	
	public void close()
	{
		try
		{
			fileOutput.close();
		}
		finally
		{
			fileOutput = null;
		}
	}
}